import React, {Component} from 'react'
import { connect } from 'react-redux'
import { Link } from 'react-router'
import { Breadcrumb, Icon } from 'antd'
import { bindActionCreators } from 'redux'
import _ from 'lodash'
import { selectMenu } from 'actions/config'

class bread extends Component{
	constructor(props) {
	    super(props);
	}

	render(){
		const { location, menus, common } = this.props;
		var breadArr = location.pathname.split('/').slice(1,100);
		var url = location.pathname;
		var obj = menus;

		// var html = breadArr.map(function(value){
		// 	obj = obj[value];
		// 	if(obj){
		// 		var display = obj.display;
		// 		obj = obj.submenu;
		// 		return (<span key={value}><span className="ant-breadcrumb-link"><span>{display}</span></span><span className="ant-breadcrumb-separator">/</span></span>)
		// 	}else{
		// 		obj = {};
		// 	}
		// });

		var selectedArr = [];
		if(common&&common.user&&common.user.menu){
			_.each(common.user.menu, (obj)=>{
				var arr = [obj.name];
				var isThis = false;
				_.each(obj.children, (lv2)=>{
					if(lv2.url == url){
						arr.push(lv2.name);
						isThis = true;
					}else if(lv2.children){
						_.each(lv2.children, (lv3)=>{
							if(lv3.url == url){
								arr.push(lv2.name);
								arr.push(lv3.name);
								isThis = true;
							}
						})
					}
				})
				isThis&&(selectedArr = arr);
			})
		}

		if(/news\/show/.test(url)){
			selectedArr = ['后台管理系统', '新闻列表', '新闻详情']
		}

		var html = selectedArr.map(function(value, index){
			return (<span key={index}><span className="ant-breadcrumb-link"><span>{value}</span></span><span className="ant-breadcrumb-separator">/</span></span>)
		});

		return (
		  <div className="ant-breadcrumb">
		  	<span>
		  		<span className="ant-breadcrumb-link"><Link to="/"><i className="x-icon x-icon-home-blob"></i>首页</Link></span>
		  		<span className="ant-breadcrumb-separator">/</span>
		  	</span>
		  	{ html }
		  </div>
		)
	}
}

function mapStateToProps(state) {
  return {
  	common: state.common,
    menus: state.sideBar.menus
  }
}

// function mapDispatchToProps(dispatch) {
//   return bindActionCreators({ selectMenu }, dispatch)
// }

export default connect(mapStateToProps)(bread)